package com.hzz.web.app.sys;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hzz.api.sys.ISysNewsOptionsService;
import com.hzz.api.sys.ISysNewsOptionsVoteService;
import com.hzz.api.sys.ISysNewsService;
import com.hzz.dto.sys.SysNewsDto;
import com.hzz.dto.sys.SysNewsOptionsDto;
import com.hzz.dto.sys.SysNewsOptionsVoteDto;
import com.hzz.framework.PageUtil;
import com.hzz.framework.Response;
import com.hzz.framework.annotation.NoAuthorize;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.web.controller.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(description = "新闻资讯")
@RestController("apiSysNewsController")
@Slf4j
public class SysNewsController extends BaseController {
	@Autowired
	ISysNewsService sysNewsService;
	@Autowired
	ISysNewsOptionsService sysNewsOptionsService;
	@Autowired
	ISysNewsOptionsVoteService sysNewsOptionsVoteService;
	static final String acPrefix = "/api/sys/sysNews/";

	// @RequiresPermissions("sysNews:menu")
	@NoAuthorize
	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = acPrefix + "page/{pageNum}")
	@ApiOperation(value = "信息分页(未删除)")
	public Response page(@RequestBody SysNewsDto dto, @PathVariable("pageNum") Integer pageNum) {
		log.info("page.........");
		Response result = new Response();
		try {
			dto.setPageSize(CommonConstant.PAGEROW_DEFAULT_COUNT);
			dto.setPageNum(pageNum);
			dto.setDelFlag(0);
			dto.setState(1);
			result.data = PageUtil.copy(sysNewsService.findDataIsPage(dto));
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	// @RequiresPermissions("sysNews:info")
	@NoAuthorize
	@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "info/{id}")
	@ApiOperation(value = "信息详情")
	public Response info(@PathVariable("id") Long id) {
		log.info("info.........");
		Response result = new Response();
		try {
			SysNewsDto dto = new SysNewsDto();
			dto.setId(id);
			dto.setDelFlag(0);
			dto.setState(1);
			result.data = sysNewsService.findDataById(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	/**
	 * 宣传引导投票
	 * 
	 * @param id
	 * @return
	 */
	@NoAuthorize
	@RequestMapping(method = RequestMethod.GET, value = acPrefix + "publicityInfo/{id}")
	@ApiOperation(value = "宣传引导投票信息详情")
	public Response publicityInfo(@PathVariable("id") Long id) {
		log.info("info.........");
		Response result = new Response();
		try {
			SysNewsDto dto = new SysNewsDto();
			dto.setId(id);
			dto.setDelFlag(0);
			dto.setState(1);
			SysNewsDto newsDto = sysNewsService.findDataById(dto);
			// 查询投票选项
			SysNewsOptionsDto option = new SysNewsOptionsDto();
			option.setNewsId(id);
			List<SysNewsOptionsDto> options = sysNewsOptionsService.findDataIsList(option);
			newsDto.setOptionsDto(options);
			result.data = newsDto;
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}

	/**
	 * 宣传引导投票
	 * 
	 * @param id
	 * @return
	 */
	@NoAuthorize
	@RequestMapping(method = RequestMethod.POST, value = acPrefix + "publicityVote")
	@ApiOperation(value = "宣传引导投票")
	public Response publicityVote(@ModelAttribute SysNewsOptionsVoteDto dto) {
		log.info("publicityVote.........{}", dto);
		Response result = new Response();
		try {
			// 检查参数
			if (StringUtils.isBlank(dto.getOpenId()) || dto.getNewsId() == null || dto.getOptionsId() == null) {
				return Response.error("参数错误");
			}

			// 检查是否已经投过票
			SysNewsOptionsVoteDto qdto = new SysNewsOptionsVoteDto();
			qdto.setOpenId(dto.getOpenId());
			qdto.setNewsId(dto.getNewsId());
			List<SysNewsOptionsVoteDto> list = sysNewsOptionsVoteService.findDataIsList(qdto);
			if (list != null && list.size() > 0) {
				return Response.error("您已投过此票");
			}
			result = sysNewsOptionsVoteService.saveOrUpdateData(dto);
		} catch (Exception e) {
			result = Response.error(e.getMessage());
		}
		return result;
	}
}